from apps.user import models
from xlwt import *
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def excel_export():
    """
    导出excel表格
    """
    list_obj = models.User.objects.filter(stuclass='200732')
    if list_obj:
        # 创建工作薄
        ws = Workbook(encoding='utf-8')
        w = ws.add_sheet(u"数据报表第一页")
        w.write(0, 0, "id")
        w.write(0, 1, u"姓名")
        w.write(0, 2, u"学号")
        w.write(0, 3, u"手机")
        w.write(0, 4, u"班级")
        # 写入数据
        excel_row = 1
        for obj in list_obj:
            w.write(excel_row, 0, obj.id)
            w.write(excel_row, 1, obj.name)
            w.write(excel_row, 2, obj.username)
            w.write(excel_row, 3, obj.telephone)
            w.write(excel_row, 4, obj.stuclass)
            excel_row += 1
        # # 检测文件是够存在
        # # 方框中代码是保存本地文件使用，如不需要请删除该代码
        # ###########################

        # exist_file = os.path.exists("test.xls")
        # if exist_file:
        #     os.remove(r"test.xls")
        # ws.save("test.xls")
        # ############################
        # sio = StringIO.StringIO()
        # ws.save(sio)
        # sio.seek(0)
        # response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-students_excels')
        # response['Content-Disposition'] = 'attachment; filename=test.xls'
        # response.write(sio.getvalue())
        path = os.path.join(BASE_DIR, 'studentSystemt', 'studentSystemt', 'media', 'students_excels','200732.xlsx')
        ws.save(path)
        return 'okok'
def get_123():
    print(123456)